package com.liam.apppush.client.getui;

import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.http.IGtPush;
import com.liam.apppush.config.GeTuiConfig;
import com.liam.apppush.util.HttpUtil;
import com.liam.apppush.util.Sha256Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * v2
 *      1 批量推送      每次不能超过 200人
 *      2 设置/解绑 标签 一批人设置一个标签，一次不超过200人   ---此功能需要申请开通才可以使用，tag的长度也有限制
 *      3 使用标签推送
 *      4 全推
 */
@Slf4j
@Component
public class GeTui_V2Client extends GeTuiConfig {
    private static  String BASE = "https://restapi.getui.com/v2/%s";
    private static  String ACCESS_TOKEN_URL = BASE + "/auth";
    private static  String TAG_URL = BASE + "/user/custom_tag/batch/%s";
    private static  String PUSH_TAG_URL = BASE + "/push/fast_custom_tag";
    private static  String CREATE_MESSAGE_URL = BASE + "/push/list/message";
    private static  String PUSH_CIDS_URL = BASE + "/push/list/cid";




    /**
     * 获取 token
     * {"msg":"success","code":0,"data":{"expire_time":"1605326484444","token":"9c3aa0d8898cdb0ea3317e0703c92923a0f7b276f311a85122503bd8be56832e"}}
     * @return
     * @throws IOException
     */
    public String getAccessToken() throws IOException {
        String url = String.format(ACCESS_TOKEN_URL,appId);
        String time = System.currentTimeMillis() + "";
        String sign = Sha256Util.getSHA256StrJava(appKey+time+masterSecret);
        JSONObject param = new JSONObject();
        param.put("sign",sign);
        param.put("timestamp",time);
        param.put("appkey",appKey);

        JSONObject result = HttpUtil.httpPost(url,param);
        if(result != null && result.getInteger("code") == 0){
            long ex = result.getJSONObject("data").getLong("expire_time") - System.currentTimeMillis();
            log.info("过期时间:{}秒",ex/1000); // 默认一天 86400
            return result.getJSONObject("data").getString("token");
        }
        return null;
    }


    /**
     * 添加标签
     * @param tag
     * @param cids
     * @throws IOException
     */
    public void addTag(String tag, List<String> cids) throws IOException {
        String url = String.format(TAG_URL,appId,tag);
        log.info(url);

        JSONObject param = new JSONObject();
        param.put("cid",cids);

        Map<String,String> heads = new HashMap<>();
        heads.put("token","c9ac9394dc24b1b380d0045814e86427c361aabc2fb77d9323f4006ab4d7d232");

        JSONObject result = HttpUtil.httpPut(url,heads,param);

    }

    /**
     * 解除标签
     * @param tag
     * @param cids
     * @throws IOException
     */
    public void deleteTag(String tag, List<String> cids) throws IOException {
        String url = String.format(TAG_URL,appId,tag);
        log.info(url);

        JSONObject param = new JSONObject();
        param.put("cid",cids);

        Map<String,String> heads = new HashMap<>();
        heads.put("token","c9ac9394dc24b1b380d0045814e86427c361aabc2fb77d9323f4006ab4d7d232");

        JSONObject result = HttpUtil.httpDelete(url,heads,param);

    }

    /**
     *  按标签发送消息
     *  {
     *     "request_id":"请填写requestid",
     *     "group_name":"请填写任务组名",
     *     "settings":{
     *         "ttl":3600000
     *     },
     *     "audience":{
     *         "fast_custom_tag":"xxxx"
     *     },
     *     "push_message":{
     *         "notification":{
     *             "title":"请填写通知标题",
     *             "body":"请填写通知内容",
     *             "click_type":"url",
     *             "url":"https//:xxx"
     *         }
     *     }
     * }
     * @param tag
     * @throws IOException
     */
    public void pushTag(String tag) throws IOException {
        String url = String.format(PUSH_TAG_URL,appId );
        log.info(url);

        Map<String,String> heads = new HashMap<>();
        heads.put("token","c9ac9394dc24b1b380d0045814e86427c361aabc2fb77d9323f4006ab4d7d232");

        JSONObject param = new JSONObject();
        JSONObject audience = new JSONObject();
        JSONObject push_message = new JSONObject();
        JSONObject notification = new JSONObject();
        param.put("request_id",System.currentTimeMillis());
        param.put("audience",audience);
        param.put("push_message",push_message);

        audience.put("fast_custom_tag",tag);

        push_message.put("notification",notification);
        notification.put("title","");
        notification.put("body","");
        notification.put("click_type","url");
        notification.put("url","https://xxx");

        JSONObject result = HttpUtil.httpPost(url,heads,param);

    }


    /**
     * 创建消息体，批量发送的前置
     * @throws IOException
     *
     * {
     *     "request_id":"请填写requestid",
     *     "group_name":"请填写任务组名",
     *     "settings":{
     *         "ttl":3600000
     *     },
     *     "push_message":{
     *         "notification":{
     *             "title":"请填写通知标题",
     *             "body":"请填写通知内容",
     *             "click_type":"url",
     *             "url":"https//:xxx"
     *         }
     *     }
     * }
     *
     */
    public String createMessage() throws IOException {
        String url = String.format(CREATE_MESSAGE_URL,appId );
        log.info(url);

        Map<String,String> heads = new HashMap<>();
        heads.put("token","c9ac9394dc24b1b380d0045814e86427c361aabc2fb77d9323f4006ab4d7d232");

        JSONObject param = new JSONObject();
        JSONObject push_message = new JSONObject();
        JSONObject notification = new JSONObject();
        param.put("request_id",System.currentTimeMillis());
        param.put("push_message",push_message);

        push_message.put("notification",notification);
        notification.put("title","");
        notification.put("body","");
        notification.put("click_type","url");
        notification.put("url","https://xxx");

        JSONObject result = HttpUtil.httpPost(url,heads,param);
        return result.getJSONObject("data").getString("taskid");
    }


    /**
     * 批量推送
     * @return
     * @throws IOException
     */
    public void pushCids(List<String> cIds) throws IOException {
        String url = String.format(PUSH_CIDS_URL,appId );
        log.info(url);

        Map<String,String> heads = new HashMap<>();
        heads.put("token","c9ac9394dc24b1b380d0045814e86427c361aabc2fb77d9323f4006ab4d7d232");

        JSONObject param = new JSONObject();
        JSONObject audience = new JSONObject();
        param.put("taskid",createMessage());
        param.put("audience",audience);
        param.put("is_async",true); // 是否异步推送，异步推送不会返回data

        audience.put("cid",cIds);

        JSONObject result = HttpUtil.httpPost(url,heads,param);
    }







}
